home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Apple Developer Connection Student Program
/
ADC Tools Sampler CD Disk 3 1999.iso
/
Documentation
/
Books
/
Learn Java on the Macintosh
/
Learn Java Projects
/
08.06 - next prime 3
/
NextPrime3.java
< prev
next >
Wrap
Text File
|
1996-04-22
|
842b
|
40 lines
/* -------------------------------------------------------------
This applet finds the primes between 1 and 100.
Java's classes: Applet (applet)
System (lang)
Custom classes: IsOdd
------------------------------------------------------------- */
public class NextPrime3 extends java.applet.Applet {
public void init() {
int primeIndex, candidate, i, last;
boolean isPrime;
System.out.println( "Prime #1 is 2." );
candidate = 3;
primeIndex = 2;
while ( primeIndex <= 100 ) {
isPrime = true;
last = (int)Math.sqrt( candidate );
for ( i = 3; (i <= last) && isPrime; i += 2 ) {
if ( (candidate % i) == 0 )
isPrime = false;
}
if ( isPrime ) {
System.out.println( "Prime " + primeIndex + " is " + candidate );
primeIndex++;
}
candidate += 2;
}
}
}